/* Heapsort
 * Пирамидальная сортировка */
#include <stdio.h>
#include <stdlib.h>

static int
heapsort(array, L, R)
	long long int *array;
	long long L;
	long long R;
{
	long long tmp, x = *(array + (L+R) / 2);
	long long i = L, j = R;

	do {
		while ( *(array + i) < x )
			++i;
		while ( *(array + j) > x )
			--j;
		if (i <= j) {
			tmp = *(array + i);
			*(array + i) = *(array + j);
			*(array + j) = tmp;
			++i;
			--j;
		}
	} while (i <= j);

	if (L < j)	
		(void)heapsort(array, L, j);
	if (i < R)
		(void)heapsort(array, i, R);

	return 0;
}


int
main (int argc, char **argv)
{
	long long *array, size = 1;
	register long long i = 0;
	char delimiter = ' ';

	if (argc == 1) {
		(void)fprintf(stderr, "Wtf?\n");
		exit(EXIT_FAILURE);
	}
	size = atol(argv[1]); /* setting amount of elements to sort via cmdline */
	array = malloc(sizeof(long long int) * (size_t)size); /* <<<<< MALLOC <<<< */

	if (array == NULL) {
		(void)fprintf(stderr, "%s: Out of memory\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	while (i < size && scanf("%lld", &array[i++]) > 0);

	(void)heapsort(array, 0, size - 1);

	for (i = 0; i < size; ++i) {
		(void)printf("%lld%c", *(array + i), delimiter);
	}
	putchar('\n');

	free(array); /* cleanup */
	exit(EXIT_SUCCESS);
}
